/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.icub3d.nephiteexchange.client;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ConvertPanel extends VerticalPanel implements ClickHandler,
		ProcessJSONRequest, KeyPressHandler {

	protected HTML conversionLabel = new HTML();
	protected Label errorLabel;

	/* The form for asking for a conversion. */
	protected ListBox ISONameListBox = new ListBox();
	protected TextBox amountBox = new TextBox();
	protected Button convertButton = new Button("Convert");
	protected HorizontalPanel formPanel = new HorizontalPanel();

	/* These are going to be used for obtaining data. */
	JSONRequest requestor = null;

	public ConvertPanel(Label errorLabel) {
		super();

		setStyleName("convertPanel");
		ISONameListBox.setStyleName("ISONameListBox");
		amountBox.setStyleName("amountBox");
		convertButton.setStyleName("convertButton");
		formPanel.setStyleName("convertFormPanel");
		conversionLabel.setStyleName("conversion");
		
		this.errorLabel = errorLabel;

		requestor = new JSONRequest(this.errorLabel, this);

		ISONameListBox.setVisibleItemCount(1);
		requestor.RequestJSON("type/currencies");

		convertButton.addClickHandler(this);
		amountBox.addKeyPressHandler(this);

		formPanel.add(ISONameListBox);
		formPanel.add(amountBox);
		formPanel.add(convertButton);

		//conversionLabel.setText("Submit a conversion!");

		add(formPanel);
		add(conversionLabel);
	}

	@Override
	public void onClick(ClickEvent event) {
		process();
	}

	@Override
	public void processJSONRequest(String response) {
		if (response.contains("currency")) {
			JsArray<ISOName> currencies = asArrayOfISOName(response);
			for (int i = 0; i < currencies.length(); i++) {
				ISONameListBox.addItem(currencies.get(i).getISOName());
			}
		} else {
			printConversion(asConversion(response));
		}
	}

	private void printConversion(JsArray<Conversion> ca) {
		Conversion c = ca.get(0);

		String output = "<b>" + amountBox.getText() + " "
				+ ISONameListBox.getValue(ISONameListBox.getSelectedIndex())
				+ "</b> in nephite coinage is <br />";

		if (c.getLimnah() != 0) {
			output += c.getLimnah() + " limnah <br />";
		}

		if (c.getShum() != 0) {
			output += c.getShum() + " shum <br />";
		}

		if (c.getSeon() != 0) {
			output += c.getSeon() + " seon <br />";
		}

		if (c.getAntion() != 0) {
			output += c.getAntion() + " antion <br />";
		}

		if (c.getSenine() != 0) {
			output += c.getSenine() + " senine <br />";
		}

		if (c.getShiblon() != 0) {
			output += c.getShiblon() + " shiblon <br />";
		}

		if (c.getShiblum() != 0) {
			output += c.getShiblum() + " shiblum <br />";
		}

		if (c.getLeah() != 0) {
			output += c.getLeah() + " leah <br />";
		}

		conversionLabel.setHTML(output);
		amountBox.setText("");
	}

	private final native JsArray<ISOName> asArrayOfISOName(String json) /*-{
		return eval(json);
	}-*/;

	private final native JsArray<Conversion> asConversion(String json) /*-{
		return eval(json);
	}-*/;

	@Override
	public void onKeyPress(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			process();
		}
	}
	
	protected void process() {
		requestor.RequestJSON("type/convert/iso-name/"
				+ ISONameListBox
						.getValue(ISONameListBox.getSelectedIndex())
				+ "/amount/" + amountBox.getText());
	}
}
